<template>
  <div class="pink-theme">
    <div class="my-detail">
      <!-- 粉色系轮播图 -->
      <van-swipe class="pink-swipe" :autoplay="3000" indicator-color="#ff85a2">
        <van-swipe-item v-for="(item,i) in product.picList" :key="i">
          <van-image :src="APIDomainName + item.md" />
        </van-swipe-item>
      </van-swipe>

      <!-- 粉色卡片式商品信息 -->
      <div class="pink-info-card">
        <van-cell 
          v-for="(field, index) in productFields" 
          :key="index"
          :title="field.label" 
          :value="product[field.key]"
          class="pink-cell"
        />
      </div>

      <!-- 粉色购买区域 -->
      <div class="pink-buy-area">
        <van-cell class="pink-cell">
          <template #default>
            <div class="count-buy">
              <div class="pink-label">购买数量</div>
              <van-stepper 
                v-model="count" 
                integer 
                button-size="22px"
                input-width="40px"
                class="pink-stepper"
              />
              <van-button 
                type="primary" 
                size="small" 
                @click="onClickAddShopcart"
                class="pink-button"
              >
                <van-icon name="shopping-cart-o" /> 加入购物车
              </van-button>
            </div>
          </template>
        </van-cell>
      </div>

      <van-button 
        type="primary" 
        size="large" 
        @click="$router.back()"
        class="pink-back-button"
      >
        <van-icon name="arrow-left" /> 返回
      </van-button>
    </div>
  </div>
</template>

<script>
import { getProductDetailByIdAPI } from "@/api/product.js";
import dayjs from 'dayjs'
import { addCartAPI } from "@/api/cart.js";
import { showToast } from 'vant'

export default {
  data() {
    return {
      product: {},
      count: 1,
      productFields: [
        { label: '商品编号', key: 'id' },
        { label: '商品名称', key: 'name' },
        { label: '商品标题', key: 'title' },
        { label: '商品规格', key: 'spec' },
        { label: '硬盘', key: 'disk' },
        { label: 'CPU', key: 'cpu' },
        { label: '内存', key: 'memory' },
        { label: '显卡', key: 'video_card' },
        { label: '分辨率', key: 'resolution' },
        { label: '上架时间', key: 'shelf_time' },
        { label: '单价', key: 'price' }
      ]
    }
  },
  mounted() {
    this.loadData();
  },
  methods: {
    loadData() {
      const id = this.$route.query.id
      getProductDetailByIdAPI(id).then(result => {
        this.product = result.data;
        this.product.shelf_time = dayjs(this.product.shelf_time).format('YYYY-MM-DD HH:mm:ss');
      })
    },
    onClickAddShopcart() {
      let params = {
        product_id: this.product.id,
        count: this.count,
      };
      addCartAPI(params).then(result => {
        showToast({
          message: result.msg,
          className: 'pink-toast'
        });
      }).catch(() => {});
    },
  }
}
</script>

<style lang="less" scoped>
.pink-theme {
  background-color: #fff5f7;
  min-height: 100vh;
  padding: 10px;

  .pink-swipe {
    border-radius: 12px;
    overflow: hidden;
    box-shadow: 0 4px 12px rgba(255, 182, 193, 0.3);
    margin-bottom: 15px;
    
    .van-image {
      width: 100%;
      height: 250px;
      display: block;
    }
  }

  .pink-info-card {
    background: linear-gradient(135deg, #ffdfe6, #ffffff);
    border-radius: 12px;
    padding: 10px;
    margin-bottom: 15px;
    box-shadow: 0 2px 8px rgba(255, 182, 193, 0.2);
  }

  .pink-cell {
    background-color: transparent;
    
    &:not(:last-child)::after {
      border-bottom: 1px dashed #ffb6c1;
    }
    
    .van-cell__title {
      color: #d23669;
      font-weight: 500;
    }
    
    .van-cell__value {
      color: #ff6b81;
    }
  }

  .pink-buy-area {
    background: linear-gradient(135deg, #ffdfe6, #ffffff);
    border-radius: 12px;
    padding: 10px;
    margin-bottom: 15px;
    box-shadow: 0 2px 8px rgba(255, 182, 193, 0.2);
  }

  .count-buy {
    display: flex;
    align-items: center;
    justify-content: space-between;
    
    .pink-label {
      color: #d23669;
      font-weight: 500;
    }
    
    .pink-stepper {
      .van-stepper__minus,
      .van-stepper__plus {
        color: #ff4757;
        border-color: #ff4757;
      }
    }
  }

  .pink-button {
    background: linear-gradient(to right, #ff8a98, #ff6b8b);
    border: none;
    border-radius: 20px;
    color: white;
    box-shadow: 0 2px 6px rgba(255, 107, 129, 0.4);
    
    &:active {
      transform: scale(0.95);
    }
  }

  .pink-back-button {
    background: linear-gradient(to right, #ffb6c1, #ffc0cb);
    border: none;
    border-radius: 25px;
    color: #d23669;
    margin-top: 20px;
    box-shadow: 0 2px 8px rgba(255, 182, 193, 0.3);
    
    &:active {
      transform: scale(0.98);
    }
  }
}

// 全局粉色Toast样式
:deep(.pink-toast) {
  background: linear-gradient(to right, #ff8a98, #ff6b8b);
  color: white;
  border-radius: 20px;
}
</style>